<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_main_class extends AWS_CONTROLLER
{
	var $versions = array();
	var $db_version = 0;
	var $db_engine = '';
	
	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'black';
		$rule_action['actions'] = array();
		
		return $rule_action;
	}

	public function setup()
	{
		@set_time_limit(0);
		
		// 升级程序禁止任何输入
		unset($_POST);
		
		define('ROOT_PATH', realpath(AWS_PATH . '../'));
		
		/*if (!is_dir(ROOT_PATH . '/install/db/upgrade/'))
		{
			H::redirect_msg('请上传最新版本的 install 目录.'));
		}*/
		
		if (!is_really_writable(AWS_PATH))
		{
			H::redirect_msg('目录 ' . AWS_PATH . ' 无法写入, 请修改目录权限');
		}
		
		$trash_files = array(
			ROOT_PATH . '/app/account/controller/c_login_class.inc.php',
			ROOT_PATH . '/app/account/controller/c_captcha_class.inc.php',
			ROOT_PATH . '/app/account/controller/c_register_class.inc.php',
			ROOT_PATH . '/app/account/controller/c_valid_class.inc.php',
			ROOT_PATH . '/app/account/controller/c_active_class.inc.php',
		);
		
		foreach ($trash_files AS $trash_file)
		{
			if (file_exists($trash_file))
			{
				if (!@unlink($trash_file))
				{
					$exists_trash_files[] = $trash_file;
				}
			}
		}

		if (is_array($exists_trash_files))
		{
			H::redirect_msg('老版本的一些文件已经不再使用, 请删除下列文件: <br />' . implode($exists_trash_files, '<br />'));
		}
		
		// 在此标记有 SQL 升级的版本号, 名称为上一个版本的 Build 编号
		$this->versions = array(
			20120608,
			20120615,
			20120622,
			20120629,
			20120706,
			20120713,
			20120719,
			20120720
		);
		
		if (!$this->db_version = get_setting('db_version'))
		{
			$this->db_version = 20120608;
		}
		
		if (!in_array($this->db_version, $this->versions))
		{
			H::redirect_msg('您的程序已经是最新版本');
		}
		
		TPL::assign('static_url', G_STATIC_URL);
		
		if (!$this->db_engine = get_setting('db_engine'))
		{
			$this->db_engine = 'InnoDB';
		}
	}
	
	public function index_action()
	{
		TPL::assign('db_version', $this->db_version);
		TPL::output('install/upgrade');
	}
	
	public function sql_action()
	{
		$sql_file = ROOT_PATH . '/app/upgrade/db/' . str_replace('..', '', $_GET['id']) . '.sql';
			
		if (file_exists($sql_file))
		{	
			$sql_query = file_get_contents($sql_file);
		}
		
		if (trim($sql_query))
		{
			$sql_query .= "\n\nUPDATE `[#DB_PREFIX#]system_setting` SET `value` = 's:8:\"" . $_GET['id'] . "\";' WHERE `varname` = 'db_version';";
			
			header('Content-type: text/plain; charset=UTF-8');
			
			echo str_replace(array('[#DB_PREFIX#]', '[#DB_ENGINE#]'), array(AWS_APP::config()->get('database')->prefix, $this->db_engine), $sql_query);
			die;
		}
	}
	
	public function run_action()
	{
		foreach ($this->versions AS $version)
		{
			$sql_query = null;
			
			$sql_file = ROOT_PATH . '/app/upgrade/db/' . $version . '.sql';
			
			if ($this->db_version <= $version AND file_exists($sql_file))
			{	
				$sql_query = file_get_contents($sql_file);
			}
			
			if (trim($sql_query))
			{
				if ($sql_error = $this->model('upgrade')->run_query($sql_query))
				{
					TPL::assign('sql_error', $sql_error);
					TPL::assign('version', $version);
					TPL::output('install/upgrade_fail');
					die;
				}
			}
		
			/*$this->model('setting')->set_vars(array(
				'db_version' => $version
			));*/
			
			if ($version == 20120706)
			{
				$this->model('publish')->update_attach_statistics();
			}
			else if ($version == 20120720)
			{
				$this->model('upgrade')->upgrade_user_action_history();
			}
		}
		
		$this->model('setting')->set_vars(array(
			'db_version' => G_VERSION_BUILD
		));
		
		H::redirect_msg('升级完成, 您的程序已经是最新版本<!-- Analytics --><img src="http://www.anwsion.com/analytics/?build=' . G_VERSION_BUILD . '&amp;site_name=' . urlencode(get_setting('site_name')) . '&amp;base_url=' . urlencode(get_setting('base_url')) . '&amp;php=' . PHP_VERSION . '" alt="" width="1" height="1" /><!-- / Analytics -->', '/');
	}
}